#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.${artifactId}.web;



import java.util.HashMap;
import java.util.Map;

/**
 * @Author: shenyang
 * @CreateTime: 2022-09-30  16:27
 * @Description: TODO
 * @Version: 1.0
 */
public class ResultData<T>   {
    public static Integer SUCCESS = 0;
    public static Integer FAIL = 1;
    private Integer code;
    private T data;
    private String msg;
    private Map<String,Object> expandData = new HashMap<String,Object>();

    public ResultData() {
        this.code = FAIL;
        this.msg = "网络异常，请稍后重试！";
    };

    public ResultData(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultData(int code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //成功直接返回
    public static ResultData ok() {
        return new ResultData(SUCCESS, "操作成功!");
    }

    //成功 返回数据
    public static ResultData ok(Object data,String... returnInfo) {
        if(returnInfo.length > 0){
            return new ResultData(SUCCESS,returnInfo[0],data);
        }else{
            return new ResultData(SUCCESS,"操作成功！",data);
        }
    }

    //失败直接返回 ResultData.fail()
    public static ResultData fail() {
        return new ResultData(ResultData.FAIL, "服务器异常!");
    }

    //失败直接返回 ResultData.fail(msg)
    public static ResultData fail(String msg) {
        return new ResultData(ResultData.FAIL, msg);
    }

    //失败直接返回 ResultData.fail(msg)
    public static ResultData fail(Integer code,String msg) {
        return new ResultData(code, msg);
    }

    //失败 根据code返回错误原因
    public static ResultData fail(Integer code){
        return new ResultData().initCodeAndDesp(code);
    }

    public ResultData initCodeAndDesp(int code){
        this.code = code;
        this.msg = ResultDataDetail.resultMap.get(String.valueOf(code));
        return this;
    }

    public ResultData initCodeAndDesp(int code,String msg){
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResultData initCodeAndDesp(){
        this.code = ResultData.SUCCESS;
        this.msg = "操作成功！";
        return this;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getExpandData() {
        return expandData;
    }

    public void setExpandData(Map<String, Object> expandData) {
        this.expandData = expandData;
    }

    @Override
    public String toString() {
        return "ResultData{" +
                "code=" + code +
                ", data=" + data +
                ", msg='" + msg + '${symbol_escape}'' +
                ", expandData=" + expandData +
                '}';
    }


}

